require(["./config"], () => {
  require(["template", "header", "jquery", "footer"], template => {
    class cart {
      constructor() {
        this.getLoca();
        this.getMoney();
        this.getChange();
        this.setNumber();
      }
      //获取localstorage 里面数据  渲染页面
      getLoca() {
        const info = JSON.parse(localStorage.getItem("info"));
        this.info = info;
        $("#cart_list").html(template("shoppingList", { info }));
        this.getCheck();
      }
      //购物车全选与单选
      getCheck() {
        let list = this;
        $("#cart_list").on("click", "#all-check", function () {
          $(".checks").prop("checked", $(this).prop("checked"));
        });
        $("#cart_list").on("click", ".checks", function () {
          const cart = Array.from(document.getElementsByClassName("checks"));

          const res = cart.every(item => {
            return $(item).prop("checked");
          });
          if (res) {
            $("#all-check").prop("checked", true);
          } else {
            $("#all-check").prop("checked", false);
          }
          list.getMoney();
        });
      }
      //计算总价
      getMoney() {
        var money = this.info.reduce((shop, item) => {
          if (item.check) shop += item.price * item.count;
          //console.log(item.check);
          return shop;
        }, 0);
        //console.log(money);
        $("#zongjia").html(money);
      }
      //改变check与总价的联系
      getChange() {
        const _this = this;
        $(".checks").on("change", function () {
          const id = Number($(this).parents("tr").attr("data-id"));
          _this.info = _this.info.map(item => {
            if (item.id === id) {
              item.check = $(this).prop("checked");
            }
            return item;
          });
          //localStorage跟外面数据保持一致
          localStorage.setItem("info", JSON.stringify(_this.info));
          //重新计算总价
          _this.getMoney();
        });
      }
      //数量加减
      setNumber() {
        $(".minus").on("click", e => {
          let num = $(e.target.nextElementSibling).val() - 0;
          num--;
          if (num <= 1) num = 1;
          $(e.target.nextElementSibling).val(num);
          let minus = $(".minus");
          let index = Array.from(minus).indexOf(e.target);
          let info = JSON.parse(localStorage.getItem("info"));
          info[index].count = num;
          info = JSON.stringify(info);
          localStorage.setItem("info", info);
        });

        $(".plus").on("click", e => {
          let num_one = $(e.target.previousElementSibling).val() - 0;
          num_one++;

          $(e.target.previousElementSibling).val(num_one);

          let plus = $(".plus");
          let index1 = Array.from(plus).indexOf(e.target);

          let info = JSON.parse(localStorage.getItem("info"));
          info[index1].count = num_one;
          info = JSON.stringify(info);
          localStorage.setItem("info", info);
        });
      }
    }

    return new cart();
  });
});
